package com.huatai.exchange.integration.base.impl;

import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.DateFormatUtils;
import com.faneui.integration.http.HttpClientUtils;
import com.huatai.base.facade.bean.workday.CheckIsWorkDayResp;
import com.huatai.exchange.integration.base.AbstractBaseService;
import com.huatai.exchange.integration.base.service.WorkDayService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;

@Service
public class WorkDayServiceImpl extends AbstractBaseService implements WorkDayService {

    private static final Logger logger = LoggerFactory.getLogger(WorkDayService.class);

    @Override
    public boolean checkIsWorkDay(String natureDay) throws BusinessException {
        String url = baseUrl + "workday/check/" + natureDay;
        CheckIsWorkDayResp resp = HttpClientUtils.get(url, CheckIsWorkDayResp.class);
        return "1".equals(resp.getIsWoryDay());
    }

    @Override
    public String getWorkDayNextWorkDay(String workDay) throws BusinessException {
        try {
            Calendar c = Calendar.getInstance();
            c.setTime(DateFormatUtils.yyyyMMdd.parse(workDay));
            do  {
                c.add(Calendar.DATE, 1);
                workDay = DateFormatUtils.yyyyMMdd.format(c.getTime());
            } while ((!checkIsWorkDay(workDay))) ;
        } catch (Exception ex) {
            logger.error("", ex);
            throw new BusinessException(ex.getMessage());
        }
        return workDay;
    }

    @Override
    public String getNextWorkDay(String natureDay) throws BusinessException {
        try {
            Calendar c = Calendar.getInstance();
            c.setTime(DateFormatUtils.yyyyMMdd.parse(natureDay));
            while (!checkIsWorkDay(natureDay)) {
                c.add(Calendar.DATE, 1);
                natureDay = DateFormatUtils.yyyyMMdd.format(c.getTime());
            }
        } catch (Exception ex) {
            logger.error("", ex);
            throw new BusinessException(ex.getMessage());
        }
        return natureDay;
    }

    @Override
    public String getNextNatureDay(String natureDay) throws BusinessException {
        try {
            Calendar c = Calendar.getInstance();
            c.setTime(DateFormatUtils.yyyyMMdd.parse(natureDay));
            c.add(Calendar.DATE, 1);
            return DateFormatUtils.yyyyMMdd.format(c.getTime());
        } catch (Exception ex) {
            logger.error("", ex);
            throw new BusinessException(ex.getMessage());
        }
    }

    @Override
    public String getLastWorkDay(String natureDay) throws BusinessException {
        try {
            Calendar c = Calendar.getInstance();
            c.setTime(DateFormatUtils.yyyyMMdd.parse(natureDay));
            do {
                c.add(Calendar.DATE, -1);
                natureDay = DateFormatUtils.yyyyMMdd.format(c.getTime());
            } while (!checkIsWorkDay(natureDay));
        } catch (Exception ex) {
            logger.error("", ex);
            throw new BusinessException(ex.getMessage());
        }
        return natureDay;
    }

    @Override
    public String getLastNatrueDay(String natueDay) throws BusinessException {
        try {
            Calendar c = Calendar.getInstance();
            c.setTime(DateFormatUtils.yyyyMMdd.parse(natueDay));
            c.add(Calendar.DATE, -1);
            return DateFormatUtils.yyyyMMdd.format(c.getTime());
        } catch (Exception ex) {
            logger.error("", ex);
            throw new BusinessException(ex.getMessage());
        }
    }
}
